//
//  GameOverLayer.cpp
//  MathSpeed
//
//  Created by ToanTM on 5/18/14.
//
//

#include "GameOverLayer.h"
#include "GameConfig.h"

GameOverLayer* GameOverLayer::create(int score, int highScore, CCNode* buttonListener,  SEL_MenuHandler playCallback, SEL_MenuHandler menuCallback){
    GameOverLayer* ret = new GameOverLayer();
    ret->setScore(score);
    ret->setHighScore(highScore);
    ret->buttonListener = buttonListener;
    ret->buttonListener->retain();
    ret->playCallback = playCallback;
    ret->menuCallback = menuCallback;
    ret->init();
    ret->autorelease();
    return ret;
}

GameOverLayer::~GameOverLayer(){
    buttonListener->release();
}

bool GameOverLayer::init(){
    if (!CCLayer::init()) {
        return false;
    }
    
    CCSprite * mainBackground = CCSprite::create(IMAGE_BACKGROUND_MAIN);
    mainBackground->setAnchorPoint(ccp(0, 0));
    addChild(mainBackground);
    
    CCSize mWinSize = CCDirector::sharedDirector()->getWinSize();
    
    CCLabelTTF* lbTitle = CCLabelTTF::create("GAME OVER", FONT_BERLIN_SANS_BOLD, 65);
//    lbTitle->setColor(ccc3(216, 242, 113));
    lbTitle->setPosition(ccp(mWinSize.width/2, mWinSize.height - 194));
    addChild(lbTitle);
    
    lbScore = CCLabelTTF::create(getStringForScore(), FONT_BERLIN_SANS_REGULAR, 58);
    lbScore->setPosition(ccp(mWinSize.width/2, lbTitle->getPosition().y - 110));
    addChild(lbScore);
    
    lbHighScore = CCLabelTTF::create(getStringForHighScore(), FONT_BERLIN_SANS_REGULAR, 58);
    lbHighScore->setPosition(ccp(mWinSize.width/2, lbScore->getPosition().y - 70));
    addChild(lbHighScore);
    
    CCMenuItemSprite* btnPlay = CCMenuItemSprite::create(CCSprite::create(IMAGE_BUTTON_PLAY_NORMAL), CCSprite::create(IMAGE_BUTTON_PLAY_PRESS), buttonListener, playCallback);
    CCMenuItemSprite* btnMenu = CCMenuItemSprite::create(CCSprite::create(IMAGE_BUTTON_MENU_NORMAL), CCSprite::create(IMAGE_BUTTON_MENU_PRESS), buttonListener, menuCallback);
    
    CCMenu* menu = CCMenu::create(btnPlay, btnMenu, NULL);
    menu->alignItemsHorizontallyWithPadding(95);
    menu->setPosition(ccp(mWinSize.width/2, lbHighScore->getPosition().y - 162));
    addChild(menu);
    return true;
}

const char* GameOverLayer::getStringForScore(){
    return CCString::createWithFormat("Score: %d", score)->getCString();
}

const char* GameOverLayer::getStringForHighScore(){
    return CCString::createWithFormat("Best: %d", highScore)->getCString();
}
void GameOverLayer::setHighScore(int highScore){
    this->highScore = highScore;
}

void GameOverLayer::setScore(int score){
    this->score = score;
}

void GameOverLayer::refreshData(){
    lbScore->setString(getStringForScore());
    lbHighScore->setString(getStringForHighScore());
}
